/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.widget;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import org.tnmk.common.util.ImageUtils;
import org.tnmk.worktime.config.AppConfig;
import org.tnmk.worktime.gui.MainFrame;

/**
 *
 * @author tnmkhoi
 */
public class SystrayIcon {
  private static SystrayIcon INSTANCE;//
  Logger log = Logger.getLogger(this.getClass().getSimpleName() + "@" + this.hashCode());
  private MainFrame mainFrame;
  private TrayIcon trayIcon;
  private SystrayIcon(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
    this.init();
  }
  /**
   * This method is used for outside to create a systrayIcon for a frame.
   * @param mainFrame 
   */
  public static void initiate(MainFrame mainFrame){
    if (INSTANCE == null) {
      INSTANCE = new SystrayIcon(mainFrame);
    }
  }

  private void init() {
    //Check the SystemTray is supported
    if (!SystemTray.isSupported()) {
      System.out.println("SystemTray is not supported");
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();

    PopupMenu menu = new PopupMenu();
    Image image = loadImageFromClasspath();
    
    trayIcon = new TrayIcon(image, AppConfig.getTitle(), menu);
    trayIcon.setImageAutoSize(true);
    trayIcon.addMouseListener(new java.awt.event.MouseAdapter() {
      @Override
      public void mouseClicked(java.awt.event.MouseEvent e) {
        if (e.getClickCount() == 2 && !e.isConsumed()) {
          showMainFrame();
        }
      }
    });

    MenuItem showItem = new MenuItem("Show");
    showItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        showMainFrame();
      }
    });
    menu.add(showItem);
    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        mainFrame.exit();
      }
    });
    menu.add(exitItem);

    try {
      tray.add(trayIcon);
    } catch (AWTException e) {
      System.out.println("TrayIcon could not be added.");
    }
  }
  public static void remove(){
    INSTANCE.removeIcon();
  }
  private void removeIcon(){
    SystemTray tray = SystemTray.getSystemTray();
    tray.remove(trayIcon);
  }
  private void showMainFrame() {
    this.mainFrame.setVisible(true);
  }

  private static Image loadImageFromClasspath() {
    return ImageUtils.loadFromClassPath("img/Product.png");
  }

  /**
   * @return the mainFrame
   */
  public MainFrame getMainFrame() {
    return mainFrame;
  }

  /**
   * @param mainFrame the mainFrame to set
   */
  public void setMainFrame(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
  }

}
